<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class admin extends CI_Controller{
    protected $_templates;
	function __construct()
	{
        parent::__construct();
          $this->pre_message = "";        
          $this->_module_name = $this->router->fetch_module();
          $this->session->set_userdata(array('Url'=>uri_string()));
          $this->lang->load('access');
          $this->load->model('admin_model','admin');
      }
      
      function index($page=0)
	  {
          $this->acl->check('view','','',base_url());
          
          $data['title'] = lang('adm.list');
          if($this->acl->check('add'))
            $data['add'] = $this->_module_name.'/admin/add';
          
          $config['base_url'] = base_url().'admin/listadmin/';  
          $config['total_rows']   =  $this->admin->getNumAdmin();
          $data['num'] = $config['total_rows'];
          $config['per_page']  =   20;
          $config['uri_segment'] = 3; 
          $this->pagination->initialize($config);   
          $data['list'] =   $this->admin->getAllAdmin($config['per_page'],$page);
          $data['pagination']    = $this->pagination->create_links();    
          $data['current_page']=$page;
          $data['_module_name']=$this->_module_name;
          
          $this->_templates['page'] = 'admin/listadmin';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      function buildData($data,$flag=0){
          $dataUpdate= array();
          $dataUpdate['login_name']=$data['login_name'];
          $dataUpdate['email']=$data['email'];
          if($data['password']!=''){
            $dataUpdate['password']=md5($data['password']);
          }
          $dataUpdate['group_id']=$data['group_id'];
          if($flag){
              $dataUpdate['dt_create']=date('Y-m-d H:i:s');
          }
          return $dataUpdate;
      }
      function add()
	  {              
          $this->acl->check('add','','',base_url());
  
          $data['title'] = lang('adm.add');
          $data['list'] = $this->admin->get_list_permit();
		  $data['list_group']=$this->admin->getAllGroup();
          if(!empty($_POST['bt_submit']))
		  {
             $this->form_validation->set_rules('login_name',lang('adm.login_name'),'trim|required|callback__check_admin');  
             $this->form_validation->set_rules('email',lang('adm.email'),'trim|required|valid_email');  
             $this->form_validation->set_rules('password',lang('adm.password'),'trim|required');  
             $this->form_validation->set_rules('re_Passwd',lang('adm.retype_password'),'trim|required|matches[password]'); 
             if($this->form_validation->run() == FALSE)
			 {
                $this->pre_message = validation_errors();
             }
			 else
			 {
                 $dataSave = $this->buildData($this->input->post(),1);
                 if($this->admin->saveAdmin(0,$dataSave))
				 {
                     $this->session->set_flashdata('message',lang('admin.save_successful'));
                     redirect($this->_module_name.'/admin');
                 }
				 else
				 {
                     $this->pre_message = lang('admin.save_unsuccessful');
                 }
             } 
          }
		  
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'admin/add';
          $this->site_lib->load($this->_templates['page'],$data);
      }
	  
      function edit($id)
	  {
          $this->acl->check('edit','','',base_url());
          
          $data['item'] = $this->admin->getAdmin($id);
          $data['title'] = lang('adm.update_account')." : ".$data['item']->login_name;
		  $data['list_group']=$this->admin->getAllGroup();
          
          if(!empty($_POST['bt_submit']))
		  {
             $this->form_validation->set_rules('login_name',lang('adm.login_name'),'trim|required|callback__check_admin['.$id.']');  
             $this->form_validation->set_rules('email',lang('adm.email'),'trim|required|valid_email');  
             $this->form_validation->set_rules('password',lang('adm.password'),'trim');  
             $this->form_validation->set_rules('re_Passwd',lang('adm.retype_password'),'trim|matches[password]'); 
             if($this->form_validation->run() == FALSE)
			 {
                $this->pre_message = validation_errors();
             }
			 else
			 {
                 $dataSave = $this->buildData($this->input->post(),0);
                 if($this->admin->saveAdmin($id,$dataSave))
				 {
                     $this->session->set_flashdata('message',lang('admin.save_successful'));
                     redirect($this->_module_name.'/admin');
                 }
				 else
				 {
                     $this->pre_message = lang('admin.save_unsuccessful');
                 }
             }
             $data['login_name'] = $this->input->post('login_name');
          }
		  else
		  {
              $data['login_name'] = $data['item']->login_name;
          }
		  
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'admin/edit';
          $this->site_lib->load($this->_templates['page'],$data);
      }   
      function _check_admin($login_name,$admin_id=0)
	  {
          if($admin_id!=0)
            $this->db->where('admin_id <>',$admin_id);
          $this->db->where('login_name',$login_name);
          $query = $this->db->get('admin');
          if($query->num_rows() > 0)
		  {
              $this->form_validation->set_message('_check_admin_edit',lang('adm.login_name_exist'));
              return FALSE;
          }
		  else
		  {
              return TRUE;
          }
      }      
      
      function del($id,$page=0)
	  {
          $this->acl->check('del','','',base_url());
          
            if($this->admin->delete($id))
                $this->session->set_flashdata('message','Đã xóa thành công');
            else $this->session->set_flashdata('message','Xóa không thành công');
			
          redirect($this->_module_name.'/admin/index/'.$page);
      }
      
      function dels()
      {
          $this->acl->check('dels','','',base_url());
          
            if(!empty($_POST['ar_id']))
            {
                $page = (int)$this->input->post('page');
                $ar_id = $this->input->post('ar_id');
                
                if(!empty($_POST['btn_submit']))
                {
                    for($i = 0; $i < sizeof($ar_id); $i ++)
					{
                        if ($ar_id[$i])
						{
                            if($this->admin->delete($ar_id[$i]))
                                $this->session->set_flashdata('message','Đã xóa thành công');
                            else $this->session->set_flashdata('message','Xóa không thành công');
                        }
                    }
                }
            }
			
            redirect($this->_module_name.'admin/index/'.$page);
      }
  }
?>
